var ws, spage, coord;
var latitude = null;
var longitude = null;
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	spage = ws.spage;
	console.log(spage);
	coord = plus.storage.getItem('coord');
	showWating();
	if(coord){
		coord = coord.split(',');
		console.log(coord[0]+" "+coord[1])
		gethos(coord[0],coord[1]);
	}else{
		plus.geolocation.getCurrentPosition(function (p) {
			if (!p.coords) {
			 	toast('无法获取您的位置！');
			}
			latitude = p.coords.latitude;
			longitude = p.coords.longitude;
			if(longitude && longitude != ''){
				var zuob = longitude+','+latitude;
				plus.storage.setItem('coord', zuob+'');
				console.log(plus.storage.getItem('coord'));
				gethos(longitude,latitude);
			}
			plus.nativeUI.closeWaiting();	
		}, function () {}, {provider:'baidu', coordsType:'bd09ll', geocode:true});
	}
},false)
function gethos(longitude,latitude){
	console.log(apiRoot+"?m=home&c=studio&a=getHospital&latitude="+latitude+"&longitude="+longitude);
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=studio&a=getHospital",
		data : {
			latitude: latitude,
			longitude: longitude,
			hospital:1
		},
		dataType :'json',
		success :function(data){
			console.log(JSON.stringify(data));
			plus.nativeUI.closeWaiting();
			var list = '';
			$.each(data, function(k,v) {
				list += '<div class="mui-table-view table-view-cell hosList" title="'+v.title+'" aid="'+v.aid+'">'+
							'<span>'+v.title+'</span>'+
							'<span class="mui-pull-right mui-h5">'+v.distance+'</span>'+
							'<p></p><span>手机号码：'+v.phone+'</span>'+
							'<p></p><span>地址：'+v.address+'</span>'+
						'</div>';
			});
			$('.shoplist').html(list);
			$('.hosList').click(function(){
//				console.log($(this).attr('title'));return;
				tapaddres($(this).attr('aid'),$(this).attr('title'));
			})
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
}
function tapaddres(aid,title){
	console.log(aid+" "+title);
	var parentid = plus.webview.getWebviewById(spage);
	mui.fire(parentid,'change',{aid:aid,title:title});
	ws.close();
}
